home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / lbbs07 / login.c < prev    next >
C/C++ Source or Header  |  1993-06-05  |  11KB  |  544 lines

  1. /*
  2.  *    Login & New user questions routines
  3.  *    (LazyBBS Project)
  4.  *
  5.  *    Public domain: may be copied and sold freely
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13.  
  14. #include "bbs.h"    /* config */
  15. #include "miscio.h" /* level 1 */
  16.  
  17. #include "login.h"    /* prototypes */
  18. #include "msg.h"    /* is_newmail() */
  19.  
  20. /* ================================================== LOGIN ROUTINES */
  21.  
  22. BBSLOGIN user;        /* the current user */
  23. int got_it=0;        /* there's a user to save */
  24. int guest=0;        /* current user is guest */
  25. int hasnewmail=0;    /* user has new private mail */
  26.  
  27. /*
  28.  *    get_login:    load login from file
  29.  */
  30.  
  31. int get_login(void )
  32. {
  33.     FILE *lf;
  34.     char temp[BBSSTR];
  35.     char tempdigit[BBSSTR];
  36.     char start[BBSSTR];
  37.     char *next;
  38.     
  39.     lf=fopen(LOGINFILE,"r");
  40.     if(!lf) 
  41.     {
  42.         logline(1,"Can't find login file");
  43.         return BBSFAIL;
  44.     }
  45.     
  46.     sprintf(start,"%s%c%s%c", user.first_name,BBSSEPAR,user.last_name,BBSSEPAR);
  47.     
  48.     while(fgets(temp,BBSSTR,lf)!=NULL)
  49.     {
  50.  
  51.         if(!iscomment(*temp) && !strnicmp(start,temp,strlen(start)))
  52.         { /* got it */
  53.             next=findword(user.first_name,temp); 
  54.             if(next)
  55.             {
  56.                 next=findword(user.last_name,next);
  57.                 if(next)
  58.                 {
  59.                     next=findword(user.password,next);
  60.                     if(next)
  61.                     {
  62.                         next=findword(user.address,next);
  63.                         if(next)
  64.                         {                
  65.                             next=findword(user.flags,next);
  66.                             if(next)
  67.                             {
  68.                                 next=findword(tempdigit,next);
  69.                                 user.last_login=atoi(tempdigit);
  70.                                 if(next)
  71.                                 {
  72.                                     next=findword(tempdigit,next);
  73.                                     user.downkb=atoi(tempdigit);
  74.                                     if(user.last_login!=today() && user.downkb>=0)
  75.                                         user.downkb=0;
  76.                                     if(next)
  77.                                     {
  78.                                         findword(tempdigit,next);
  79.                                         user.calls=atoi(tempdigit);
  80.                                             got_it++;
  81.                                         user.calls++;
  82.                                     }
  83.                                 }
  84.                             }
  85.                         }
  86.                     }
  87.                 }
  88.             }
  89.         }
  90.     }
  91.     
  92.     fclose(lf);
  93.     
  94.     if(got_it)
  95.         return BBSOK;
  96.     return BBSFAIL;    
  97. }
  98.  
  99. /*
  100.  *     save/update login
  101.  */
  102.  
  103. int close_login(void )
  104. {
  105.     FILE *lf,*tf;
  106.     char start[BBSSTR], temp[BBSSTR];
  107.     
  108.     if(got_it && !guest)
  109.     {
  110.         lf=fopen(LOGINFILE,"r");
  111.         if(!lf) 
  112.             logline(1,"Creating login file");
  113.         
  114.         
  115.         tf=fopen(tempfile(),"w");
  116.         if(!tf) 
  117.             logline(1,"Can't open temp file");
  118.         else
  119.         {
  120.             sprintf(start,"%s%c%s", user.first_name,BBSSEPAR,user.last_name);
  121.  
  122.             if(lf)
  123.             {        
  124.                 while(fgets(temp,BBSSTR,lf)!=NULL)
  125.                 {
  126.                     if(!iscomment(*temp) && strnicmp(start,temp,strlen(start)))
  127.                     { /* not got it */
  128.                         if(fputs(temp,tf)) 
  129.                             logline(1,"write error in login file");
  130.                     }
  131.                 }
  132.             }
  133.     
  134.             fprintf(tf,"%s%c%s%c%s%c%s%c%s%c%d%c%d%c%d\n",    
  135.                     user.first_name,BBSSEPAR,user.last_name,BBSSEPAR,
  136.                     user.password,BBSSEPAR,user.address,BBSSEPAR,
  137.                     user.flags,BBSSEPAR,today(),BBSSEPAR,user.downkb,
  138.                     BBSSEPAR,user.calls);
  139.             fclose(tf);
  140.         }
  141.         remove(LOGINFILE);
  142.         
  143.         if(lf)
  144.             fclose(lf);
  145.                     
  146.         rename(tempfile(), LOGINFILE);
  147.     }
  148.     return BBSOK;
  149. }
  150.  
  151. void display_login(void )
  152. {
  153.     int diffdays;
  154.     int nmail,newnmail;
  155.     if(!got_it) return;
  156.     
  157.     diffdays=today()-user.last_login;
  158.     
  159.     if(guest)
  160.         out_printf("\n\nGuest User logged in.\n");
  161.     else
  162.         out_printf("\n\n%s %s from %s logged in.\n",
  163.             user.first_name, user.last_name, user.address);
  164.     
  165.     if(is_newmail(get_netarea(),&nmail,&newnmail)==BBSOK)
  166.     {
  167.         if(newnmail) /* bip for new mail */
  168.         {
  169.             /* set mail var */
  170.             hasnewmail++;
  171.             
  172.             out_printf("Netmail        : %d messages (%d new)\n\0x07",
  173.                 nmail,newnmail);
  174.         }
  175.         else
  176.             out_printf("Netmail        : %d messages\n",nmail);
  177.     }
  178.     
  179.     if(user.flags[0])
  180.         out_printf("User flags     : %s\n",user.flags);
  181.         
  182.     if(user.last_login==0)
  183.         out_printf("Last login     : never.\n");
  184.     else if(diffdays)
  185.         out_printf("Last login     : %d day(s) ago.\n",diffdays);
  186.     else
  187.         out_printf("Last login     : today.\n");
  188.         
  189.     out_printf("Call number    : %d.\n",user.calls);
  190.  
  191.     if(user.downkb>=0)
  192.         out_printf("Download credit: %d Kilobytes.\n\n",get_maxdown()-user.downkb);
  193.     else
  194.         out_printf("Download credit: None.\n\n");
  195.     
  196.     /* security check on 1st call */
  197.     if(nmail && (user.last_login==0))
  198.     {
  199.         guest++;
  200.         logline(4,"WARNING: New user has netmail!!!");
  201.         out_printf("\nSECURITY PROBLEM: You are a new user and mail to you has been found\n");
  202.         out_printf("in the netmail area. So, you are downgraded to guest user status.\n");
  203.         out_printf("Send a netmail to your sysop for a permanent account.\n");
  204.     }
  205.     
  206.     out_printf("\nHit [Return]\n");
  207.     getkey();
  208. }
  209.  
  210. int open_login(void )
  211. {
  212.     char temp[BBSSTR];
  213.     int try,error;
  214.     int try_1st=0;
  215.     int got_user=0;
  216.     
  217.     /* init default values for newusers/guest */
  218.     *user.flags=0;
  219.     user.calls=1;
  220.     user.last_login=0;
  221.     
  222. buggy_goto: /* fixme goto */
  223.     try=MAXTRIES;
  224.     do {
  225.         out_printf("\n\nEnter your first name (example: \"Robert\")\n>");
  226.         error=getstring(user.first_name);
  227.         if(error==BBSOK)
  228.         {    
  229.             if(strchr(user.first_name,' ')==NULL && strlen(user.first_name)>1)
  230.             {
  231.                 got_user++;
  232.                 break;
  233.             }
  234.         }
  235.         try--;
  236.     } while(try>=0 && error==BBSOK);
  237.     
  238.     if(got_user)
  239.     {
  240.         strcln(user.first_name,BBSSEPAR);
  241.         strcln(user.first_name,' ');
  242.         strlwr(user.first_name);
  243.         *user.first_name=toupper(*user.first_name);
  244.         
  245.         if(strcmp(user.first_name,"Guest")==0)
  246.         {
  247.             strcpy(user.last_name,"User");
  248.             strcpy(user.password,"-junk-");
  249.             strcpy(user.address,"Unknown");
  250.             user.downkb=-1;
  251.             got_it++;
  252.             guest++;    
  253.             logline(2,"Guest User logged in");
  254.             return BBSOK;
  255.         }
  256.         else
  257.         { /* !guest user */
  258.             try=MAXTRIES;got_user=0;
  259.             do {
  260.                 out_printf("\n\nEnter your last name (example: \"De.Niro\")\n>");
  261.                 if(BBSOK==(error=getstring(user.last_name)))
  262.                 {    
  263.                     if(strchr(user.last_name,' ')==NULL && strlen(user.last_name)>1)
  264.                     {
  265.                         got_user++;
  266.                         break;
  267.                     }    
  268.                 }
  269.             } while(try-->=0 && error==BBSOK);
  270.  
  271.             if(got_user)
  272.             {
  273.                 strcln(user.last_name,BBSSEPAR);
  274.                 strcln(user.last_name,' ');
  275.                 strlwr(user.last_name);
  276.                 *user.last_name=toupper(*user.last_name);
  277.                 
  278.                 if(get_login()==BBSOK)
  279.                 {
  280.                     try=MAXTRIES;
  281.                     while(try)
  282.                     {
  283.                         out_printf("\n\nEnter password\n>");
  284.                         if(getpasswdstring(temp)!=BBSOK)
  285.                             return BBSFAIL;
  286.                         if(strcmp(strlwr(temp),strlwr(user.password))==0)
  287.                         {
  288.                             display_login();
  289.                             logline(1,"%s %s from %s logged in.",user.first_name,user.last_name,user.address);
  290.                             return BBSOK;
  291.                         }
  292.                         logline(1,"Password error %s %s : %s",user.first_name,user.last_name,
  293.                                     temp);
  294.                         try--;
  295.                     }
  296.                     out_printf("\n\nGo play somewhere else!\n");
  297.                     return BBSFAIL;
  298.                 }
  299.                 out_printf("\n\n%s %s not in user base, do you want to login as a new user [Y/n]?",
  300.                     user.first_name,user.last_name);
  301.                 if(tolower(getkey()=='n'))
  302.                 {
  303.                     if(try_1st==0)
  304.                     {
  305.                         try_1st++;
  306.     /* fixme */            goto buggy_goto;
  307.                     }
  308.                     else
  309.                         return BBSFAIL;
  310.                 }
  311.                 else
  312.                 {    /* new user */
  313.                     try=MAXTRIES;
  314.                     while(try)
  315.                     {
  316.                         out_printf("\n\nChoose a password\n>");
  317.                         if(getpasswdstring(temp)==BBSFAIL)
  318.                             return BBSFAIL;
  319.                         out_printf("\n\nOnce again...\n>");
  320.                         if(getpasswdstring(user.password)==BBSFAIL)
  321.                             return BBSFAIL;
  322.                         if(strcmp(strlwr(temp),strlwr(user.password))==0)
  323.                             break;
  324.                         try--;
  325.                     }
  326.                     if(!try)
  327.                         logline(1,"Password matching failed");
  328.                     out_printf("\n\nEnter your city and country (example: \"Novosibirsk, Russia\")\n>");
  329.                     if(getstring(user.address)==BBSOK)
  330.                     {
  331.                         user_questions();
  332.                         user.downkb=get_maxdown()-get_newdown();
  333.                         got_it++;
  334.                         display_login();
  335.                         logline(1,"New user %s %s from %s logged in",
  336.                                         user.first_name,user.last_name,user.address);
  337.                         return BBSOK;
  338.                     }
  339.                 }
  340.             }
  341.         } /* !guest */
  342.     }
  343.     return BBSFAIL;
  344. }
  345.  
  346. int change_password(void )
  347. {
  348.     char pw[BBSSTR],pw2[BBSSTR];
  349.     
  350.     out_printf(CLS_STRING);
  351.     logline(3,"User changing password");
  352.     
  353.     if(!got_it)
  354.         return BBSFAIL;
  355.     if(guest)
  356.     {
  357.         out_printf("Guest user can't change password!!\n\n[Return]\n");
  358.         getkey();
  359.         return BBSOK;
  360.     }
  361.     
  362.     out_printf("\n\nChoose a password\n>");
  363.     if(getpasswdstring(pw)==BBSFAIL)
  364.         return BBSFAIL;
  365.     if(strlen(pw)<3)
  366.     {
  367.         printf("\nPassword too short, change canceled! Hit a key.\n");
  368.         getkey();
  369.         return BBSFAIL;
  370.     }
  371.     out_printf("\n\nOnce again...\n>");
  372.     if(getpasswdstring(pw2)==BBSFAIL)
  373.         return BBSFAIL;    
  374.     
  375.     if(strcmp(pw,pw2)==0)
  376.     {
  377.         logline(2,"Password changed");
  378.         strcpy(user.password,pw);
  379.         out_printf("\n\nPassword changed, squish [Return].\n");
  380.         getkey();
  381.         return BBSOK;
  382.     }
  383.     
  384.     out_printf("\nPasswords don't match?\n[Return]\n");
  385.     getkey();
  386.     return BBSFAIL;
  387. }
  388.  
  389. int get_ulastlogin(void )
  390. {
  391.     if(got_it)
  392.         return user.last_login;
  393.     else
  394.         return 0;
  395. }
  396.  
  397. static char nstore[2*BBSSTR+1];
  398.  
  399. char *get_uname(void )
  400. {
  401.     if(!got_it)
  402.         strcpy(nstore,"Unknown");
  403.     
  404.     strcpy(nstore, user.first_name);
  405.     strcat(nstore, " ");
  406.     strcat(nstore, user.last_name);
  407.     return nstore;
  408. }
  409.  
  410. int get_ucredit(int kilobytes)
  411. {
  412.     if(!got_it || user.downkb<0)
  413.         return BBSFAIL;
  414.         
  415.     if(kilobytes<(get_maxdown()-user.downkb)) /* ok, for standard file */
  416.     {
  417.         user.downkb+=kilobytes;
  418.         return BBSOK;
  419.     }
  420.  
  421.     if(kilobytes>get_maxdown() && user.downkb==0) /* allows 1 file >limit /day excluding all other */
  422.     {
  423.         user.downkb=get_maxdown();
  424.         return BBSOK;
  425.     }
  426.         
  427.     return BBSFAIL; /* nope */
  428. }
  429.  
  430. int get_uflag(char flag)
  431. {
  432.     int i=0;
  433.     while(user.flags[i])
  434.     {
  435.         if(user.flags[i]==flag)
  436.             return BBSOK;
  437.         i++;
  438.     }
  439.     return BBSFAIL;
  440. }
  441.  
  442. int is_uguest(void )
  443. {
  444.     return guest;
  445. }
  446.  
  447. int has_new_mail(void )
  448. {
  449.     return hasnewmail;
  450. }
  451.  
  452. void reset_new_mail(void )
  453. {
  454.     int junk,newm;
  455.     
  456.     hasnewmail=0;
  457.     
  458.     if(is_newmail(get_netarea(),&junk,&newm)==BBSOK)
  459.     {
  460.         if(newm) /* bip for new mail */
  461.             hasnewmail++;
  462.     }
  463. }
  464.  
  465. /* ============================================== USER QUESTION ROUTINE */
  466.  
  467. int user_questions()
  468. {
  469.     FILE *question;
  470.     FILE *answer;
  471.     char *temp=malloc(BBSSTR*5+1);
  472.     char ansstr[BBSSTR];
  473.     char *ptr;
  474.     int qnb=1;
  475.     int errorfl=0;
  476.     
  477.     if(!temp)
  478.     {
  479.         logline(1,"no more memory for user_questions");
  480.         return BBSFAIL;
  481.     }
  482.     
  483.     question=fopen(QUESTFILE,"r");
  484.     if(!question)
  485.     {
  486.         logline(1,"Can't read user questions file");
  487.         errorfl++;
  488.     }
  489.     else
  490.     {
  491.         answer=fopen(ANSWERFILE,"a");
  492.         if(!answer)
  493.         {
  494.             logline(1,"Can't open answers file");
  495.             errorfl++;
  496.         }
  497.         else
  498.         {
  499.             fprintf(answer,"----- New User %s answering new user questions:\n",get_uname());
  500.     
  501.             out_printf("\n");
  502.             while(fgets(temp,BBSSTR*5,question)!=NULL)
  503.             {
  504.                 if(!iscomment(*temp))
  505.                 {
  506.                     strcln(temp,-1); /* remove end \n */
  507.                 
  508.                     /* replace all "\n" by <cr><lf> */
  509.                     ptr=strstr(temp,"\\n");
  510.                     while(ptr) 
  511.                     {
  512.                         ptr[0]=0x0d;
  513.                         ptr[1]=0x0a;
  514.                         ptr=strstr(ptr+2,"\\n");
  515.                     }
  516.             
  517.                     out_printf("\n\n%s\n\n>",temp);
  518.             
  519.                     if(getstring(ansstr)==BBSFAIL)
  520.                     {
  521.                         errorfl++;
  522.                         break;
  523.                     }
  524.                     else
  525.                     {
  526.                         fprintf(answer, "%d : %s\n", qnb, ansstr);
  527.                         qnb++;
  528.                     }
  529.                 }
  530.             }
  531.     
  532.             fclose(answer);
  533.         }
  534.         fclose(question);
  535.     }
  536.     
  537.     free(temp);
  538.     
  539.     if(errorfl)
  540.         return BBSFAIL;
  541.         
  542.     return BBSOK;
  543.